import time
from pysqlite2 import dbapi2 as sqlite

DBHOST = 'localhost'
DBNAME = 'sysmetrics.db'
DBUSER = ''
DBPASSWD = ''

class DBMgrError(Exception):
    """
    Custom exception class 
    """
    pass

class DBMgr:

    def __init__(self, dbhost=None, dbname=None, dbuser=None, dbpasswd=None):
        if not dbhost:
            dbhost = DBHOST
        self._dbhost = dbhost
        if not dbname:
            dbname = DBNAME
        self._dbname = dbname
        if not dbuser:
            dbuser = DBUSER
        self._dbuser = dbuser
        if not dbpasswd:
            dbpasswd = DBPASSWD
        self._dbpasswd = dbpasswd

    def connect(self):
        try:
            self._conn = sqlite.connect(self._dbname)
            print "Connecting to database..."
            time.sleep(2)
            print "Connected to database."
        except sqlite.OperationalError, e:
            #print e
            raise DBMgrError, e

    def commit(self):
        self._conn.commit()
        print "Committing"

    def execute(self, sql):
        self._cursor = self._conn.cursor()
        self._cursor.execute(sql)
        print "Executing:", sql
        time.sleep(2)
        print "Done executing:", sql
        self._conn.commit()

    def select(self, sql):
        #cursor = self._connection.cursor()
        self.execute(sql)
        return self._cursor.fetchall()

if __name__ == "__main__":
    db = DBMgr()
    db.connect()
